package com.surelution.lansao.wxmp.partner

import com.surelution.lansao.Member
import com.surelution.lansao.Partner
import com.surelution.lansao.PartnerMember
import com.surelution.lansao.PartnerRequiredOperation;
import com.surelution.lansao.wxmp.BaseLansaoAction
import com.surelution.lansao.wxmp.IdRefuser
import com.surelution.whistle.core.Attribute


class SubscribeAction extends BaseLansaoAction {

	/* (non-Javadoc)
	 * @see com.surelution.whistle.core.BaseAction#accept()
	 */
	@Override
	public boolean accept() {
		("Hello2BizUser" == getParam(Attribute.KEY_Content)) || ("event" == getParam(Attribute.KEY_MsgType) && "subscribe" == getParam("Event"))
	}

	/* (non-Javadoc)
	 * @see com.surelution.whistle.core.BaseAction#execute()
	 */
	@Override
	public void execute() {
		StringBuffer content = new StringBuffer("感谢关注维新订餐\n")
		def now = new Date()
		def wxOpenId = getParam(Attribute.KEY_FromUserName)
		def partner = Partner.findByWxOpenId(wxOpenId)
		if(!partner) {
			partner = new Partner(wxOpenId: wxOpenId, active:false)
			partner.save(flush:true)
			content.append("您是第一次关注我们，我们需要收集您的相关信息，以完成注册过程。请根据引导，一步一步操作。这些信息我们仅会用于注册您的账户。同时，请您务必填写真实信息，如果由于您填写的信息不真实准确，造成微信公司停用您的账号，我们将无能为力！\n")
			content.append("请回复您的姓名（身份证上写明的姓名）：\n")
			PartnerRequiredOperation operation = new PartnerRequiredOperation(partner:partner, className:PartnerNameRequiredSub.class, forever:true)
			operation.save(flush:true)
		} else {
			content.append("我们已经拥有您的全部信息，维新订餐祝您愉快！\n")
		}
		String slId = IdRefuser.getRefusedId(partner.id)
		put(new Attribute(Attribute.KEY_Content, "感谢关注蓝嫂午餐直送[${slId}]"))
	}

}
